/**
 * FileUtils.java
 *
 * @author: ZhuJiahui
 * @date: 2018/7/26 23:38
 */
package com.zhujiahui.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.io.input.BOMInputStream;
import org.apache.commons.lang3.StringUtils;

/**
 * @description:
 * @author: ZhuJiahui
 * @version: 1.0
 */
public class FileUtils {

    public static final int CREATE_RETURN_SUCCESS = 0;

    public static final int CREATE_RETURN_ERROR = -1;

    public static final int WRITE_RETURN_SUCCESS = 0;

    public static final int WRITE_RETURN_ERROR = -1;

    /**
     * 默认构造方法
     */
    public FileUtils() {

    }

    /**
     * 创建文件路径
     *
     * @param directory
     *            文件路径名
     * @return 是否成功的标识符
     */
    public static int createDirectory(String directory) {
        File file = new File(directory);

        if (!file.exists()) {
            try {
                file.mkdir();
            } catch (SecurityException e) {
                e.printStackTrace();
                return CREATE_RETURN_ERROR;
            } catch (Exception e) {
                e.printStackTrace();
                return CREATE_RETURN_ERROR;
            }
        }

        return CREATE_RETURN_SUCCESS;
    }

    /**
     * 获取文件路径下的所有文件
     *
     * @param directory
     *            文件路径名
     * @return 文件名列表
     */
    public static List<String> getDirectoryFiles(String directory) {
        List<String> fileList = new ArrayList<String>();

        if (directory == null) {
            return fileList;
        }

        File directoryFile = new File(directory);
        File[] files = directoryFile.listFiles();

        for (File file : files) {
            if (file.isFile()) {
                fileList.add(file.getAbsolutePath());
            }
        }

        return fileList;
    }

    /**
     * 将文件读到一个一维String列表
     *
     * @param fileName
     *            文件名
     * @return 一维String List
     */
    public static List<String> readToString1DL(String fileName) {

        List<String> dataList = new ArrayList<String>();

        if (fileName == null) {
            return dataList;
        }

        FileInputStream fis = null;
        BOMInputStream bis = null;
        BufferedReader br = null;

        try {
            fis = new FileInputStream(fileName);
            bis = new BOMInputStream(fis);
            br = new BufferedReader(new InputStreamReader(bis, "UTF-8"));

            // 按行读取
            String lineString = br.readLine();
            while (lineString != null) {
                dataList.add(lineString);
                lineString = br.readLine();
            }
            br.close();
        } catch (FileNotFoundException e) {
            System.out.println("文件没有找到。");
            e.printStackTrace();
            return new ArrayList<String>();
        } catch (IOException e) {
            System.out.println("文件读取错误。");
            e.printStackTrace();
            return new ArrayList<String>();
        }

        return dataList;
    }

    /**
     * 将文件读到一个二维String列表
     *
     * @param fileName 文件名
     * @param delimiter 文件行分隔符
     * @return 二维String List
     */
    public static List<List<String>> readToString2DL(String fileName, String delimiter) {

        List<List<String>> dataList = new ArrayList<List<String>>();

        if (fileName == null) {
            return dataList;
        }

        FileInputStream fis = null;
        BOMInputStream bis = null;
        BufferedReader br = null;

        try {
            fis = new FileInputStream(fileName);
            bis = new BOMInputStream(fis);
            br = new BufferedReader(new InputStreamReader(bis, "UTF-8"));

            // 按行读取
            String lineString = br.readLine();
            while (lineString != null) {
                String[] lineSplits = StringUtils.splitByWholeSeparatorPreserveAllTokens(lineString, delimiter);

                if (lineSplits.length > 0) {
                    List<String> lineList = new ArrayList<String>();
                    for (String string : lineSplits) {
                        lineList.add(string);
                    }
                    dataList.add(lineList);
                }
                lineString = br.readLine();
            }
            br.close();
        } catch (FileNotFoundException e) {
            System.out.println("文件没有找到。");
            e.printStackTrace();
            return new ArrayList<List<String>>();
        } catch (IOException e) {
            System.out.println("文件读取错误。");
            e.printStackTrace();
            return new ArrayList<List<String>>();
        }

        return dataList;
    }

    /**
     * 写入文件
     *
     * @param fileName 文件名
     * @param dataList 数据
     * @return 是否写入成功的标识符
     */
    public static int writeString1DL(String fileName, List<String> dataList)
    {
        if (null == fileName)
        {
            System.out.println("文件名为null.");
            return WRITE_RETURN_ERROR;
        }

        FileOutputStream fos = null;
        OutputStreamWriter osw = null;
        BufferedWriter bw = null;

        try {
            fos = new FileOutputStream(fileName);
            osw = new OutputStreamWriter(fos, "UTF-8");
            bw = new BufferedWriter(osw);
            for (String lineString : dataList) {
                bw.write(lineString);
                bw.write("\n");
            }
            bw.close();
        } catch (FileNotFoundException e) {
            System.out.println("文件没有找到。");
            e.printStackTrace();
            return WRITE_RETURN_ERROR;
        } catch (IOException e) {
            System.out.println("文件读取错误。");
            e.printStackTrace();
            return WRITE_RETURN_ERROR;
        }

        return WRITE_RETURN_SUCCESS;
    }
}
